import axios from '../../request/http';
import pinyin from '@/assets/js/pinyin'
/* 汉字转拼音用法如下，拼音js还挺多，已经分离出去，不用可以删除
this.$fz.pinyin.getFullChars('上海')
this.$fz.pinyin.getCamelChars('上海')
*/
/* 配合element table，拉平对象数组、返回合并行函数，用法如下：
拉平对象数组：
this.$fz.objectFlat(
    原始数据,
    对应子列表数组,
    目标数据（要存的数组）,
    布尔值是否加入空值（默认否）
  );
合并行：在合并合数spanMethod({ row, column, rowIndex, columnIndex }) 里，返回
return this.$fz.spanMethod(row, columnIndex, this.tableData《数据》, {
        2: "subjectId",
        7: "gradeId"
      }《合并参数，对象名为多少格之前合并，对象值为合并id，即对象数组每层唯一ID》);
  */

let exportExcel = function (a, u, filename = '导出文件') {
    console.log(a, u)
    axios.post(u, a, { responseType: 'blob' }).then(res => {
        console.log(res)
        let blob = new Blob([res.data], { type: "application/vnd.ms-excel" });
        let url = URL.createObjectURL(blob);
        // window.location.href = objectUrl;
        let link = document.createElement("a");
        link.style.display = "none";
        link.href = url;
        let name = decodeURI(res.headers['content-disposition'].split('filename="')[1].split('"')[0])
        if (name) {
            link.setAttribute("download", name);
        } else {
            link.setAttribute("download", filename + ".xlsx");
        }
        document.body.appendChild(link);
        link.click();
    });
}
let objFlat = function (arr, level, c, empty, i, a) {
    let tt = Object.assign(a, arr);
    if (i < level.length) {
        let b = arr[level[i]];
        if (b.length) {
            for (let x of b) {
                objFlat(x, level, c, empty, i + 1, Object.assign({}, tt));
            }
        } else {
            if (empty) {
                tt[level[i]] = [{}];
                c.push(tt);
            }
        }
    } else {
        c.push(tt);
    }
}
let spanMethod1 = function (row, k, d) {
    let a = d;
    for (let i of k) {
        a = a.filter(el => el[i] == row[i]);
        console.log(a)
    }
    if (a.length > 1) {
        if (!a.findIndex(el => el === row)) {
            return {
                rowspan: a.length,
                colspan: 1
            };
        } else {
            return {
                rowspan: 0,
                colspan: 1
            };
        }
    }
}
// 合并函数
let spanMethod = function (row, columnIndex, data, key) {
    if (columnIndex < parseInt(Object.keys(key)[Object.keys(key).length - 1])) {
        let keyArr = []
        for (var k in key) {
            if (columnIndex < parseInt(k)) {
                keyArr.push(key[k])
                break
            } else {
                keyArr.push(key[k])
            }
        }
        let a = spanMethod1(row, keyArr, data)
        return a;
    }
}
// 对象数组拉平函数
let objectFlat = function (arr, level, c, empty = false, ii = 0, tem = {}) {
    for (const term of arr) {
        objFlat(term, level, c, empty, ii, Object.assign({}, tem))
    }
}
export default {
    exportExcel, objectFlat, spanMethod, pinyin
}